import org.omg.PortableInterceptor.INACTIVE;

import java.util.*;

public class test1 {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer,Integer> hashTable=new HashMap<Integer,Integer>();
        for (int i = 0; i < nums.length; ++i) {
            if(hashTable.containsKey(target-nums[i])){
                return  new int[]{hashTable.get(target-nums[i]),i};
            }
            hashTable.put(nums[i],i );
        }
        return new int[0];
    }
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        for (int i = 0; i != n; ++i) {
            nums1[m + i] = nums2[i];
        }
        Arrays.sort(nums1);
    }

    public int[] intersect(int[] nums1, int[] nums2) {
        if(nums1.length> nums2.length){
            return intersect(nums2,nums1);
        }
        Map<Integer,Integer>map=new HashMap<Integer,Integer>();
        //把短的数组的元素和出现的次数放进去
        for (int num:nums1) {
            int count = map.getOrDefault(num, 0) + 1;
            map.put(num, count);
        }
        //要返回的数组
        int[] intersection = new int[nums1.length];
        int index = 0;

        for (int num : nums2) {
            int count = map.getOrDefault(num, 0);
            if (count > 0) {
                intersection[index++] = num;//把这个元素放到答案中
                count--;//出现次数-1
                if (count > 0) {
                    map.put(num, count);//put() 方法将指定的键/值对插入到 HashMap 中。
                } else {
                    map.remove(num);
                }
            }
        }
        return Arrays.copyOfRange(intersection, 0, index);
    }
    public int maxProfit(int[] prices) {
        int minPrice=Integer.MAX_VALUE;
        int maxPrice=0;
        for (int i = 0; i < prices.length; i++) {
            if(prices[i]<minPrice){
                minPrice=prices[i];
            } else if (prices[i]-minPrice>maxPrice) {
                maxPrice=prices[i]-minPrice;
            }
        }
        return maxPrice;
    }


    public boolean containsDuplicate(int[] nums) {
        Set<Integer> set = new HashSet<Integer>();
        for (int x : nums) {
            if (!set.add(x)) {
                return true;
            }
        }
        return false;
    }
}
